package cn.iocoder.yudao.module.cmt.controller.admin.base;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.cmt.controller.admin.base.vo.*;
import cn.iocoder.yudao.module.cmt.dal.dataobject.base.BaseMerchantInfoDO;
import cn.iocoder.yudao.module.cmt.service.base.BaseMerchantInfoService;

@Tag(name = "管理后台 - 商户信息")
@RestController
@RequestMapping("/cmt/base-merchant-info")
@Validated
public class BaseMerchantInfoController {

    @Resource
    private BaseMerchantInfoService merchantInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建商户信息")
    @PreAuthorize("@ss.hasPermission('cmt:base-merchant-info:create')")
    public CommonResult<Long> createMerchantInfo(@Valid @RequestBody BaseMerchantInfoSaveReqVO createReqVO) {
        return success(merchantInfoService.createMerchantInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商户信息")
    @PreAuthorize("@ss.hasPermission('cmt:base-merchant-info:update')")
    public CommonResult<Boolean> updateMerchantInfo(@Valid @RequestBody BaseMerchantInfoSaveReqVO updateReqVO) {
        merchantInfoService.updateMerchantInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商户信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cmt:base-merchant-info:delete')")
    public CommonResult<Boolean> deleteMerchantInfo(@RequestParam("id") Long id) {
        merchantInfoService.deleteMerchantInfo(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除商户信息")
    @PreAuthorize("@ss.hasPermission('cmt:base-merchant-info:delete')")
    public CommonResult<Boolean> deleteMerchantInfoList(@RequestParam("ids") List<Long> ids) {
        merchantInfoService.deleteMerchantInfoListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商户信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cmt:base-merchant-info:query')")
    public CommonResult<BaseMerchantInfoRespVO> getMerchantInfo(@RequestParam("id") Long id) {
        BaseMerchantInfoDO merchantInfo = merchantInfoService.getMerchantInfo(id);
        return success(BeanUtils.toBean(merchantInfo, BaseMerchantInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商户信息分页")
    @PreAuthorize("@ss.hasPermission('cmt:base-merchant-info:query')")
    public CommonResult<PageResult<BaseMerchantInfoRespVO>> getMerchantInfoPage(@Valid BaseMerchantInfoPageReqVO pageReqVO) {
        PageResult<BaseMerchantInfoDO> pageResult = merchantInfoService.getMerchantInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BaseMerchantInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出商户信息 Excel")
    @PreAuthorize("@ss.hasPermission('cmt:base-merchant-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMerchantInfoExcel(@Valid BaseMerchantInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BaseMerchantInfoDO> list = merchantInfoService.getMerchantInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "商户信息.xls", "数据", BaseMerchantInfoRespVO.class,
                        BeanUtils.toBean(list, BaseMerchantInfoRespVO.class));
    }

}
